package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import java.util.Date;

public class GetDate {

    public static String oldDate = null;
    public static String nowDate = null;

    public static String getOldDate() {
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        try {

            PreparedStatement ps = myCon.connection.prepareStatement(
                    "SELECT PlaceDate FROM placetime order by placedate limit 1");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                oldDate = rs.getString(1);
            }
            ps.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        return oldDate;
    }

    public static String getNowDate() {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        nowDate = ft.format(date.getTime());
        return nowDate;
    }

    public static String getFutureDate(int i) {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        return ft.format(date.getTime() + i * 3600000 * 24);
    }

    public static long getDistance() {
        if (oldDate == null || nowDate == null) {
            return -2;
        }
        long now = 0, old = 0;
        try {
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            now = ft.parse(nowDate).getTime();
            old = ft.parse(oldDate).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (now - old) / 3600000 / 24;
    }
}
